package com.yzqp.serverorder.server;


import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.yzqp.serverorder.dao.OrderDao;
import com.yzqp.serverorder.entity.OrderEntity;
import com.yzqp.serverorder.outserver.goods.GoodsOutServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class OrderServer {

    @Autowired
    OrderDao orderDao;

    @Autowired
    GoodsOutServer goodsOutServer;

    // 分布式事务
    /**
     * REQUIRED当前没有分布式事务，就创建。当前有分布式事务，就加入
     */
    /**
     * SUPPORTS当前没有分布式事务，非分布式事务运行。当前有分布式事务，就加入
     */
    @LcnTransaction()
    @Transactional
    public String addOrder(int id, int count){
        OrderEntity order = new OrderEntity();
        order.setGoodsId(id);
        order.setCount(count);
        order = orderDao.save(order);
        String s = goodsOutServer.updateStock(count, id);
        if("goodsError".equals(s)){
            throw new RuntimeException("goodsError");
            //return "goodsError";
        }
        return s;
    }
}
